<script setup>
import dayjs from 'dayjs'
import { deleteEvaluateService } from '@/apis/evaluate'

const props = defineProps({
  list: {
    type: Array,
    required: true
  }
})

// 向父组件提交更新列表事件
const emit = defineEmits(['updateList'])

// 删除数据
const deleteData = async (row) => {
  await ElMessageBox.confirm('你确定要删除该条数据吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })

  await deleteEvaluateService(row.evaluate_id)
  ElMessage.success('删除成功')
  emit('updateList')
}
</script>

<template>
  <div>
    <!-- 列表组件 -->
    <el-table
      :data="props.list"
      style="width: 100%"
      v-if="props.list?.length"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      :row-style="{ height: '100px' }"
    >
      <el-table-column width="200" type="expand">
        <template #default="{ row }">
          <div class="container">
            <div class="evaluate">
              <div class="rating">
                <div class="label">用户评分：</div>
                <el-rate
                  size="large"
                  v-model="row.rating"
                  disabled
                  text-color="#ff9900"
                />
              </div>
              <div class="comment">用户评价：{{ row.comment }}</div>
            </div>

            <div class="order">
              <div class="title">订单信息</div>
              <div class="header">
                <div class="place">名称：{{ row.order.place_name }}</div>
                <div class="total">总价：{{ row.order.total_amount }}</div>
              </div>
              <div class="detail">
                <div class="type">类型：{{ row.order.product_type }}</div>
                <div class="price">单价：{{ row.order.price }}</div>
                <div class="quantity">数量：{{ row.order.quantity }}</div>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="date" type="index" label="序号" width="60" />
      <el-table-column width="200" label="用户名">
        <template #default="{ row }">
          {{ row.user.username }}
        </template>
      </el-table-column>
      <el-table-column width="200" label="用户头像">
        <template #default="{ row }">
          <el-avatar :src="row.user.avatar" />
        </template>
      </el-table-column>
      <el-table-column width="200" label="手机号">
        <template #default="{ row }">
          {{ row.user.phone }}
        </template>
      </el-table-column>
      <el-table-column width="250" label="评价时间">
        <template #default="{ row }">
          {{ dayjs(row.createdAt).format('YYYY-MM-DD hh:mm:ss') }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="300">
        <template #default="{ row }">
          <el-button type="danger" circle @click="deleteData(row)">
            <template #icon>
              <i-ep-Delete></i-ep-Delete>
            </template>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-empty v-else :image-size="400" />
  </div>
</template>

<style lang="scss" scoped>
.container {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  padding: 0 100px 10px 100px;

  .evaluate {
    width: 45%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: flex-start;

    .rating {
      display: flex;
      justify-content: space-around;
      align-items: center;
    }

    .comment {
      display: flex;
      justify-content: space-around;
      align-items: center;
    }
  }

  .order {
    width: 40%;
    display: flex;
    flex-direction: column;

    .title {
      text-align: center;
      font-size: 18px;
      font-weight: bold;
      padding: 10px 0;
    }

    .header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 0;
    }

    .detail {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }
}
</style>
